import Vue from 'vue'
import axios from "axios"
import VueAxios from 'vue-axios'
import {router} from './router';
import controller from './controller.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
import './assets/icon/iconfont.css'
import mixin from './mixin/mixin.js'
import superMallCommons from './commons.js'

Vue.config.productionTip = false;
Vue.mixin(mixin);

var axiosInstance = axios.create({
    // baseURL: "http://localhost:1111/",
    baseURL: "http://60.221.62.216:1111/",
    timeout: 30000,
    headers: {"Content-Type": "application/json"},
    withCredentials: false
});
axiosInstance.defaults.headers = {
    'token': sessionStorage.getItem(superMallCommons.consts.sessionKeys.JWT_TOKEN),
    "Content-Type": "application/json"
};
axiosInstance.interceptors.response.use(function (response) {
        if (document.getElementsByClassName("el-loading-mask").length > 0) {
            Vue.prototype.$loading().close();
        }
        if (response.data && response.data.code) {
            if (response.data.msg == '401') {
                Vue.prototype.$message({
                    type: 'warning',
                    message: '请先登录'
                });
                window.location.href = '/#/login'
            } else if (response.data.code != 0) {
                Vue.prototype.$message({
                    type: 'warning',
                    message: response.data.msg
                });
                return response;
            }
        }
        return response;
    },
    function (error) {
        if (!error.response) {
            //window.location.href=process.env.VUE_APP_SUPERMALL_LOGIN_URL;
        } else {
            if (error.response.status == 400) {
                Vue.prototype.$message({
                    type: 'warning',
                    message: "提交的参数有误"
                });
            } else if (error.response.status == 500) {
                Vue.prototype.$message({
                    type: 'warning',
                    message: error.response.data.msg
                });
            }
        }
        return error;
    });


Vue.prototype.$axios = axiosInstance;
Vue.use(VueAxios, axios);

function createBaseRequestParamData(module) {
    var obj = new Object();
    obj.module = module;
    obj.token = sessionStorage.getItem(superMallCommons.consts.sessionKeys.JWT_TOKEN);
    return obj;
}

Vue.prototype.$createReqParamData = function (data) {
    var obj = createBaseRequestParamData("Octopus");
    obj.data = data;
    return obj;
};

Vue.use(ElementUI);

new Vue({
    router,
    el: '#controller',
    render: h => h(controller),
});
